#include "my_headerT2csJ.h"

void* workfunc(void *ret){
    threadPoll_t * tpt = (threadPoll_t *) ret;




    ClientSession css;
    memset(&css,0,sizeof(css));
    init_session(&css,NULL);




    while(1){
        pthread_mutex_lock(&tpt->pmt);
        while(tpt->flag == 0 && tpt->ltt.list_length <= 0){
            printf("thread %ld wait...\n",pthread_self());
            pthread_cond_wait(&tpt->pct,&tpt->pmt);
        }
        if(tpt->flag == 1){
            printf("thread %ld gone...\n",pthread_self());
            pthread_mutex_unlock(&tpt->pmt);
            pthread_exit(NULL);
        }

        int netfd = tpt->ltt.head->val;
        printf("thread %ld get netfd %d...\n",pthread_self(),tpt->ltt.head->val);
        headDel(&tpt->ltt);
        pthread_mutex_unlock(&tpt->pmt);





        css.netfd = netfd;
        while(1){
            char bufrecv[MAX_PATH_LEN] = {0};
            recv(css.netfd,bufrecv,sizeof(bufrecv),0);
            // 使用sscanf分割字符串
            char optname[64] = {0};
            char pathrecv[MAX_PATH_LEN] = {0};
            sscanf(bufrecv, "%s %s", optname,pathrecv);
            if(strcmp("cd",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,1,pathrecv);
            }else if (strcmp("ls",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,2,pathrecv);
            }else if(strcmp("pwd",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,3,pathrecv);
            }else if (strcmp("mkdir",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,4,pathrecv);
            }else if(strcmp("rmdir",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,5,pathrecv);
            }else{
                printf("wrong?\n");
                break; //现在输错直接退出循环不服务这个客户端了
            }
        }





        /* trans_file(netfd); */

        printf("thread %ld done...\n",pthread_self());
        close(netfd);
    }
    return 0;
}

int make_worker(int workernum,threadPoll_t *tpt){
    for(int i = 0;i<workernum;i++){
        pthread_create(&tpt->wkt.workers[i],NULL,workfunc,tpt);
    }
    return 0;
}

